package wangjg.security.util;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.Converter;

public class BeanUtil extends BeanUtilsBean{

	public Object get(Class c,String str) throws Exception {
		boolean p = isPrimitive(c);
		if(p){
			Object obj = super.convert(str, c);
			return obj;
		}
		return c.newInstance();
	}
	
	public static boolean isPrimitive(Class c){
		try {
			if(c.isPrimitive()){
				return true;
			}
			return ((Class)c.getField("TYPE").get(null)).isPrimitive();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static void main(String args[]) throws Exception{
		ConvertUtilsBean cu = new ConvertUtilsBean();
		Converter c = cu.lookup(int.class);
		Object n = c.convert(int.class, "100");
		System.out.println(n);
		
		Object o=new BeanUtil().get(int.class, "200");
		System.out.println(o);
	}
}
